package org.jeecg.modules.rooms.controller;


import cn.hutool.core.util.EnumUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.Enum.ResultCode;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.rooms.DTO.CesRoomBuildingFloorDto;
import org.jeecg.modules.rooms.Enum.CouponEnums;
import org.jeecg.modules.rooms.entity.CesRoomBuildingFloor;
import org.jeecg.modules.rooms.service.CesRoomBuildingFloorServiceImpl;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 楼栋楼层表 前端控制器
 * </p>
 *
 * @author fendo
 * @since 2023-03-06
 */
@RestController
@RequestMapping("/rooms/cesRoomBuildingFloor")
@Api(tags="楼栋")
@Slf4j
public class CesRoomBuildingFloorController {

    @Resource
    private CesRoomBuildingFloorServiceImpl buildingFloorService;

    @ApiOperation(value="楼栋查询", notes="楼栋查询")
    @GetMapping(value = "/tree")
    public Result tree(@RequestParam String hotelId){
        if(StringUtils.isBlank(hotelId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return buildingFloorService.tree(hotelId);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "创建楼栋或者楼层")
    @ApiOperation(value="创建楼栋或者楼层", notes="创建楼栋或者楼层")
    @PostMapping(value = "/save")
    public Result save(@RequestBody  CesRoomBuildingFloorDto dto){
        if(StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getParentId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getName())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getType() != null && dto.getType().intValue() != 0){
            List<Object> codes = EnumUtil.getFieldValues(CouponEnums.RoomBuildingFloorTypeEnum.class, "code");
            if(!codes.contains(dto.getType().intValue())){
                return Result.error("参数不正确!请选择正确的类型!");
            }
        }else{
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return buildingFloorService.create(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "修改楼栋或者楼层")
    @ApiOperation(value="修改楼栋或者楼层", notes="修改楼栋或者楼层")
    @PutMapping(value = "/modify")
    public Result modify(@RequestBody CesRoomBuildingFloorDto dto){
        if(StringUtils.isBlank(dto.getId())){
            return Result.error(ResultCode.PARAM_MISS,"数据ID 必传!");
        }
        if(StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getParentId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getName())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getType() != null && dto.getType().intValue() != 0){
            List<Object> codes = EnumUtil.getFieldValues(CouponEnums.RoomBuildingFloorTypeEnum.class, "code");
            if(!codes.contains(dto.getType().intValue())){
                return Result.error("参数不正确!请选择正确的类型!");
            }
        }else{
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return buildingFloorService.modify(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @AutoLog(value = "删除楼栋或者楼层")
    @ApiOperation(value="删除楼栋或者楼层", notes="删除楼栋或者楼层")
    @DeleteMapping(value = "/delete")
    public Result delete(CesRoomBuildingFloorDto dto){
        if(StringUtils.isBlank(dto.getId())){
            return Result.error(ResultCode.PARAM_MISS,"数据ID 必传!");
        }
        try{
            return buildingFloorService.delete(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value="楼栋查询", notes="楼栋查询")
    @GetMapping(value = "/queryByParentId")
    public Result queryByParentId(@RequestParam String parentId){
        if(StringUtils.isBlank(parentId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return buildingFloorService.queryByParentId(parentId);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


}

